package cn.vc.shop.service;

import cn.vc.shop.entity.dto.ShoppingCartDto;
import cn.vc.shop.entity.po.Commodity;
import cn.vc.shop.entity.po.Shop;
import cn.vc.shop.entity.po.ShoppingCart;
import cn.vc.shop.mapper.ShoppingCartMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ShoppingCartService extends ServiceImpl<ShoppingCartMapper, ShoppingCart> {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private CommodityService commodityService;


    /**
     * 添加商品
     * @param commodityId 商品ID
     * @param num 商品数量
     * @return
     */
    public boolean insert(Long userId, Long commodityId, Integer num) {
        // 1.在数据库查询购物车中是否已经存在该商品
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId).eq(ShoppingCart::getCommodityId, commodityId);
        ShoppingCart shoppingCart = getOne(lqw);
        boolean bo = false;
        // 2.判断购物车中是否已经存在
        if (shoppingCart == null) {
            // 2.1.购物车中不存在，创建一个新的
            shoppingCart = new ShoppingCart();
            shoppingCart.setUserId(userId);
            shoppingCart.setCommodityId(commodityId);
            shoppingCart.setNum(num);
            // 2.2.保存
            bo = this.save(shoppingCart);
        } else {
            // 2.1.购物车中存在，增加商品数量即可
            shoppingCart.setNum(shoppingCart.getNum()+num);
            // 2.2.修改
            bo = this.updateById(shoppingCart);
        }
        // 3.返回结果
        return bo;
    }


    /**
     * 查询指定用户ID的购物车
     * @param userId 用户ID
     * @return
     */
    public List<ShoppingCartDto> selectCartByUserId(Long userId) {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectList(lqw);
        if (shoppingCartList == null || shoppingCartList.isEmpty()) return Collections.emptyList();
        List<ShoppingCartDto> shoppingCartDtoList = shoppingCartList.stream()
                .map(c -> {
                    ShoppingCartDto shoppingCartDto = new ShoppingCartDto();
                    BeanUtils.copyProperties(c, shoppingCartDto);
                    Commodity commodity = commodityService.getById(c.getCommodityId());
                    if (commodity == null) return null;
                    shoppingCartDto.setCoverUrl(commodity.getCoverUrl());
                    shoppingCartDto.setCommodityName(commodity.getName());
                    shoppingCartDto.setPrice(commodity.getPrice());
                    return shoppingCartDto;
                })
                // 过滤空对象
                .filter(Objects::nonNull)
                // 转换为列表
                .collect(Collectors.toList());
        return shoppingCartDtoList;
    }

}
